﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TravelLink.WebModel;
using TravelLink.DAL;
using DevExpress.Web.ASPxGridView;
using TravelLink.WebApp.UserControl.Member.JobEditable;
using Interactive.Utility.Extension;

namespace TravelLink.WebApp.UserControl.Member {
    public partial class UC_Member_ModifyJob_EN : ControlBase {
        private int _ResumeId;

        public string BirthCity = "";
        public string BirthCityValue = "";

        public string PlaceCity = "";
        public string PlaceCityValue = "";

        Dictionary<string, ASPxGridView> _gridViewDict;


        JobEditableFacade facade;


        public string UserPhoto = string.Empty;

        public string BirthDay = string.Empty;

        public bool HasPhoto {
            get {
                return !string.IsNullOrEmpty(UserPhoto);
            }
        }

        private bool CheckIsCreateResume() {
            bool flag = true;
            Guid UserCode = this.CurrentUser.DBUser.UserCode;
            TL_Resume resume = null;
            if ((resume = this.DB.TL_Resume.Where(u => u.Language == 2 && u.UserCode == UserCode).FirstOrDefault()) == null) {
                flag = false;
                resume = new TL_Resume();
                resume.RowTime = DateTime.Now;
                resume.UserCode = UserCode;
                resume.Language = 2;

                DB.TL_Resume.InsertOnSubmit(resume);
                DB.SubmitChanges();

                Response.Redirect(this.Request.RawUrl);
                return flag;
            }
            _ResumeId = resume.Idx;
            return flag;

        }
        protected void Page_Load(object sender, EventArgs e) {
            if (!CheckIsCreateResume()) return;

            _gridViewDict = new Dictionary<string, ASPxGridView>(7);

            _gridViewDict.Add(aspGridSkills.ID, aspGridSkills);
            _gridViewDict.Add(aspGridEducation.ID, aspGridEducation);
            _gridViewDict.Add(this.aspxGridMembership.ID, aspxGridMembership);
            _gridViewDict.Add(aspxGridAward.ID, aspxGridAward);
            _gridViewDict.Add(aspxGridQualification.ID, aspxGridQualification);
            _gridViewDict.Add(aspxGridCareer.ID, aspxGridCareer);

            // _ResumeId = 1;

            facade = new JobEditableFacade(_gridViewDict, false, _ResumeId, this.CurrentUser.DBUser.UserCode.ToString());
            if (!IsPostBack) {
                facade.ClearSession();
            }

            facade.Initialize();



            if (!this.IsPostBack) {
                InitializeDropdown();

                TL_User user = this.DB.TL_User.FirstOrDefault(u => u.UserCode == this.CurrentUser.DBUser.UserCode);

                TL_Resume resume = this.DB.TL_Resume.FirstOrDefault(u => u.Idx == _ResumeId && u.UserCode == user.UserCode);
                UserPhoto = user.PhotoImage;
                this.txtAddress.Text = resume.Address;
                BirthDay = user.BirthDay.HasValue ? user.BirthDay.Value.ToString("yyyy-MM-dd") : string.Empty;
                this.txtBirthDay.Text = BirthDay;
                //      this.txtBirthDay.Text = user.BirthDay.HasValue ? user.BirthDay.Value.ToString("yyyy/MM/dd") : string.Empty;
                this.txtCity.Text = resume.ExpectedWorkin;
                this.txtStatus.Text = resume.Status;
                this.txtPeriod.Text = resume.NoticePerod;
                BirthCityValue = (resume.BirthCity ?? 0).ToString();
                if (resume.BirthCity.HasValue) {
                    BirthCity = DB.TL_City.Where(u => u.Idx == (resume.BirthCity ?? 0)).Select(u => u.CName).FirstOrDefault();
                }

                PlaceCityValue = (resume.PlaceCity ?? 0).ToString();
                if (resume.PlaceCity.HasValue) {
                    PlaceCity = DB.TL_City.Where(u => u.Idx == (resume.PlaceCity ?? 0)).Select(u => u.CName).FirstOrDefault();
                }
                this.txtCityDisplay.InnerHtml = resume.ExpectedWorkin;
                this.txtEmail.Text = user.Email;
                this.txtEnName.Text = (user.EFirstName ?? string.Empty) + " " + (user.ELastName ?? string.Empty);
                this.txtInterests.Text = resume.Interests;
                this.txtInterestsDisplay.InnerHtml = (resume.Interests ?? string.Empty).Replace("\n", "<br>");
                this.txtLanguageDesc.Text = resume.LanguageDesc;
                this.txtLanguageDescDisplay.InnerHtml = (resume.LanguageDesc ?? string.Empty).Replace("\n", "<br>");
                this.txtMobile.Text = resume.Mobile ?? user.Mobile;
                this.txtName.Text = (user.CFirstName ?? string.Empty) + " " + (user.CLastName ?? string.Empty);
                titlename.InnerText = this.txtName.Text;
                this.txtObjective.Text = resume.Objective;
                this.txtObjectiveDisplay.InnerHtml = (resume.Objective ?? string.Empty).Replace("\n", "<br>");
                this.txtRemark.Text = resume.Profile;
                this.txtTel.Text = user.Tel;
                this.txtZipcode.Text = user.PostCode;

                if (user.Gender.HasValue) {

                    this.chkGender_1.Checked = user.Gender ?? false;
                    this.chkGender_0.Checked = !this.chkGender_1.Checked;
                }
                if (user.IsMarried.HasValue) {

                    this.chkMarry_0.Checked = user.IsMarried ?? false;
                    this.chkMarry_1.Checked = !this.chkMarry_0.Checked;
                }

                if (user.TL_Country != null) {
                    this.dropCountry.SelectedValue = user.TL_Country.ID.ToString();
                }
            }


        }

        private void InitializeDropdown() {

            this.dropCountry.DataSource = this.DB.TL_Country.ToList();
            this.dropCountry.DataTextField = "ECountry";
            this.dropCountry.DataValueField = "ID";
            this.dropCountry.DataBind();
            dropCountry.Items.Insert(0, new ListItem("Choose Country", "-1"));


        }

        #region Grid Event

        protected void RowDeleted(object sender, DevExpress.Web.Data.ASPxDataDeletedEventArgs e) {
            e.ExceptionHandled = true;
        }

        protected void RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e) {
            facade.RowDeleting(sender, e);
        }

        protected void RowUpdated(object sender, DevExpress.Web.Data.ASPxDataUpdatedEventArgs e) {
            e.ExceptionHandled = true;

        }

        protected void RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) {
            facade.RowUpdating(sender, e);

        }

        protected void RowInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e) {
            e.ExceptionHandled = true;
        }

        protected void RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e) {

            facade.RowInserting(sender, e);
        }


        protected void aspGridSkills_RowDeleted(object sender, DevExpress.Web.Data.ASPxDataDeletedEventArgs e) {
            RowDeleted(sender, e);
        }

        protected void aspGridSkills_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e) {
            RowDeleting(sender, e);
        }

        protected void aspGridSkills_RowInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e) {
            RowInserted(sender, e);
        }

        protected void aspGridSkills_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e) {
            RowInserting(sender, e);
        }

        protected void aspGridSkills_RowUpdated(object sender, DevExpress.Web.Data.ASPxDataUpdatedEventArgs e) {
            RowUpdated(sender, e);
        }

        protected void aspGridSkills_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) {
            RowUpdating(sender, e);
        }

        #endregion

        protected void btnSubmit_Click(object sender, ImageClickEventArgs e) {

            string photos = WebHelper.GetFormStr("imgFileValues");
            int birthCity = WebHelper.GetFormInt("valBirthPlace", 0);
            int placeCity = WebHelper.GetFormInt("valPlaceResidences", 0);
            TL_Resume resume = this.DB.TL_Resume.FirstOrDefault(u => u.Idx == _ResumeId && u.UserCode == this.CurrentUser.DBUser.UserCode);
            TL_User user = this.DB.TL_User.FirstOrDefault(u => u.UserCode == this.CurrentUser.DBUser.UserCode);

            resume.Address = this.txtAddress.Text;
            string dt = this.txtBirthDay.Text;
            if (dt != "") {

                user.BirthDay = Convert.ToDateTime(dt);
            }
            else {
                user.BirthDay = null;
            }

            resume.ExpectedWorkin = this.txtCity.Text;
            user.Email = this.txtEmail.Text;
            user.EFirstName = this.txtEnName.Text;
            resume.Interests = this.txtInterests.Text;
            resume.LanguageDesc = this.txtLanguageDesc.Text;
            resume.Mobile = this.txtMobile.Text;
            user.CFirstName = this.txtName.Text;
            user.PhotoImage = photos;
            resume.Objective = this.txtObjective.Text;
            resume.Profile = this.txtRemark.Text;
            user.Tel = this.txtTel.Text;

            user.PostCode = this.txtZipcode.Text;
            user.Gender = this.chkGender_1.Checked;
            user.IsMarried = this.chkMarry_0.Checked;
            user.CountryId = this.dropCountry.SelectedValue.AsInt();
            resume.NoticePerod = this.txtPeriod.Text;
            resume.Status = this.txtStatus.Text;
            resume.BirthCity = birthCity;
            resume.PlaceCity = placeCity;
            resume.RowTime = DateTime.Now;
            this.DB.SubmitChanges();
            facade.Save(resume);
            UserPhoto = photos;
            Response.Redirect("MemberViewJob_EN.aspx");
        }
    }
}